package com.peenongsaban.freelance.service;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Vector;

import com.peenongsaban.freelance.bean.AptitudeTypeBean;
import com.peenongsaban.freelance.bean.PaymentOfServiceBean;
import com.peenongsaban.freelance.bean.PostServiceBean;
import com.peenongsaban.freelance.dao.ListServiceDAO;
import com.peenongsaban.freelance.dao.impl.ListServiceDAOImpl;
import com.peenongsaban.freelance.meta.AddressMeta;
import com.peenongsaban.freelance.meta.ListPostJobViewMeta;
import com.peenongsaban.freelance.meta.PostServiceMeta;
import com.peenongsaban.freelance.meta.ServiceMeta;
import com.peenongsaban.freelance.meta.UserMeta;
import com.peenongsaban.freelance.model.ListServiceModel;
import com.peenongsaban.freelance.utils.StringUtils;

public class ListServiceService {
	public Vector<AptitudeTypeBean> getListAptitudeType() {
		ListServiceDAO dao = new ListServiceDAOImpl();
		Vector<AptitudeTypeBean> vector = dao.getListAptitudeType();
		return vector;
	}

	public ListServiceModel getListServiceByCondition(
			ListServiceModel model) {
		ListServiceModel newModel = new ListServiceModel();
		ArrayList<String> sqlCondition = new ArrayList<String>();

		// Keyword
		String keyword = model.getSearchForm().getKeyword();
		if (!StringUtils.isEmptyOrNull(keyword)) {
			Scanner keywordScanner = new Scanner(keyword);
			StringBuffer keywordBuf = new StringBuffer();
			while (keywordScanner.hasNext()) {
				keywordBuf.append(ServiceMeta.COL_SERVICE_NAME + " LIKE '%"
						+ keywordScanner.next() + "%' OR ");
			}
			if (keywordBuf.length() != 0) {
				keywordBuf.replace(keywordBuf.length() - 4,
						keywordBuf.length(), "");
			}
			String kwStr = keywordBuf.toString();
			sqlCondition.add(kwStr);
			newModel.getSearchForm().setKeyword(keyword);
		}
		
		//Aptitude Type 
		String typeId = model.getSearchForm().getAptitudeType();
		if (!StringUtils.isEmptyOrNull(typeId)) {
			sqlCondition.add(ListPostJobViewMeta.COL_APTITUDE_TYPE_ID
					+ " = " + typeId);
			newModel.getSearchForm().setAptitudeType(typeId);
		}

		// Trust Level
		String trustLevel = model.getSearchForm().getTrust();
		if (!StringUtils.isEmptyOrNull(trustLevel)) {
			sqlCondition.add(UserMeta.COL_TRUST_LEVEL + " = "
					+ trustLevel);
			newModel.getSearchForm().setTrust(trustLevel);
		}

		// Work Area
		String workArea = model.getSearchForm().getWorkArea();
		if (!StringUtils.isEmptyOrNull(workArea)) {
			sqlCondition.add(AddressMeta.COL_PROVINCE + " = '"
					+ workArea + "'");
			newModel.getSearchForm().setWorkArea(workArea);
		}

		String sql = "";
		int pageNo = model.getPageCurrentNo();
		if (sqlCondition.size() != 0) {
			sql = " WHERE " + sql;
		}

		// Condition SQL command
		for (int i = 0; i < sqlCondition.size(); i++) {
			sql = sql + sqlCondition.get(i);
			if ((i + 1) != sqlCondition.size()) {
				sql = sql + " AND ";
			}
		}
		String orderBy = model.getSearchForm().getOrderBy();
		if(!StringUtils.isEmptyOrNull(orderBy)){
			String order = " ORDER BY ";
			if(orderBy.equals("1")){
				order += PostServiceMeta.COL_POST_DATE+" DESC";
			}else if(orderBy.equals("2")){
				order += PostServiceMeta.COL_AMOUNT_REQUEST_POST+" DESC";
			}else if(orderBy.equals("3")){
				order += UserMeta.COL_COUNT_REQUEST_JOB+" DESC";
			}	
			sql += order;
		}

		// SQL count data size
		String sqlSizeData = sql;

		// Condition SQL command for paging
		int itemList = 1;
		if (pageNo == 0) {
			sql = sql + " LIMIT 0," + itemList;
		} else {
			sql = sql + " LIMIT " + ((pageNo - 1) * itemList) + "," + itemList;
		}

		System.out.println(sql);

		//Add List service by condition
		ListServiceDAO dao = new ListServiceDAOImpl();
		Vector<PostServiceBean> vectorListService = dao.getListServiceByCondition(sql);
		double minPriceMoney = 0.0;
		double maxPriceMoney = 0.0;
		double minPriceCoin = 0.0;
		double maxPriceCoin = 0.0;
		for (int i = 0; i < vectorListService.size(); i++) {
			String seviceDetail = vectorListService.get(i).getService()
					.getDescriptionOfService();
			if (seviceDetail.length() >= 300) {
				vectorListService.get(i)
						.getService()
						.setDescriptionOfService(
								seviceDetail.substring(0, 300) + "...");
			} else {
				vectorListService.get(i).getService()
						.setDescriptionOfService(seviceDetail);
			}
			Vector<PaymentOfServiceBean> paymentVector = new Vector<PaymentOfServiceBean>();
			paymentVector = vectorListService.get(i).getService()
					.getListPaymentOfService();
			for (int j = 0; j < paymentVector.size(); j++) {
				if (paymentVector.get(j).getPaymentType().getPaymentTypeID() == 1) {
					if (paymentVector.get(j).getPrice() > maxPriceMoney) {
						maxPriceMoney = paymentVector.get(j).getPrice();
					} else if (paymentVector.get(j).getPrice() < minPriceMoney) {
						minPriceMoney = paymentVector.get(j).getPrice();
					}
					if (minPriceMoney == 0.0) {
						vectorListService.get(i).getService()
								.setPriceRange(maxPriceMoney + " บาท");
					} else {
						vectorListService.get(i)
								.getService()
								.setPriceRange(
										minPriceMoney + " - " + maxPriceMoney
												+ " บาท");
					}
					break;
				} else if (paymentVector.get(j).getPaymentType()
						.getPaymentTypeID() == 2) {
					if (paymentVector.get(j).getPrice() > maxPriceCoin) {
						maxPriceCoin = paymentVector.get(j).getPrice();
					} else if (paymentVector.get(j).getPrice() < minPriceCoin) {
						minPriceCoin = paymentVector.get(j).getPrice();
					}
					if (minPriceCoin == 0) {
						vectorListService.get(i).getService()
								.setPriceRange(maxPriceCoin + " เหรียญ");
					} else {
						vectorListService.get(i)
								.getService()
								.setPriceRange(
										minPriceCoin + " - " + maxPriceCoin
												+ " เหรียญ");
					}
					break;
				} else if (paymentVector.get(j).getPaymentType()
						.getPaymentTypeID() == 3) {
					//TODO Night ยังไม่ได้แก้
				}
				
			}

		}
		
		newModel.setListServiceByCondition(vectorListService);

		//Add list aptitude type
		Vector<AptitudeTypeBean> vectorListAptitude = dao.getListAptitudeType();
		newModel.setListAptitudeType(vectorListAptitude);

		//Add list list service last update
		Vector<PostServiceBean> vectorLastService = dao.getListServiceLastUpdate();
		for (int i = 0; i < vectorLastService.size(); i++) {
			String seviceName = vectorLastService.get(i).getService()
					.getServiceName();
			if (seviceName.length() >= 35) {
				vectorLastService.get(i)
						.getService()
						.setServiceName(
								seviceName.substring(0, 35) + "...");
			} else {
				vectorLastService.get(i).getService()
						.setServiceName(seviceName);
			}
		}
		

		newModel.setListServiceLastUpdate(vectorLastService);
		
		// Display paging
		int size = dao.countTotalJob(sqlSizeData);
		int totalPage = (size / itemList);
		if (size % itemList != 0) {
			totalPage++;
		}
		if (pageNo == 0) {
			newModel.setPageCurrentNo(1);
		} else {
			newModel.setPageCurrentNo(pageNo);
		}

		newModel.setListFirstRowNo(((newModel.getPageCurrentNo()-1)*itemList)+1);
		newModel.setListLastRowNo(newModel.getPageCurrentNo()*itemList);
		newModel.setTotalPage(totalPage);
		newModel.setListTotalRow(size);


		return newModel;
	}

	public Vector<PostServiceBean> getListServiceLastUpdate() {
		ListServiceDAO dao = new ListServiceDAOImpl();
		Vector<PostServiceBean> vector = dao.getListServiceLastUpdate();
		return vector;
	}
}
